package com.mk.springboot.controller;

import com.mk.springboot.until.Result;
import com.mk.springboot.until.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @RequestMapping("/user/login")
    @ResponseBody
    public Result login(HttpServletRequest httpServletRequest, HttpSession session) throws InterruptedException {
        String username = httpServletRequest.getParameter("username");
        String password = httpServletRequest.getParameter("password");
        Thread.sleep(500); // 为了尝试前端转圈圈的效果-。-//
        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
            session.setAttribute("loginUser", username);
            return ResultUtil.success();
        } else {
            return ResultUtil.warning("账号或者密码错误，请重新输入！");
        }
    }

    @RequestMapping("/user/sign-out")
    public String signOut(HttpSession session) {
        session.removeAttribute("loginUser");
        return "login.html";
    }
}
